<!--
 * @Author: firesky
 * @Date: 2020-09-02 15:29:00
 * @LastEditTime: 2020-11-05 09:43:06
 * @LastEditors: firesky
-->
<template>
  <div>
    <el-row class="admin-top">
      <el-button icon="el-icon-plus" type="success" @click="dialogAdd">添加</el-button>
      <el-button icon="el-icon-edit" type="success" @click="dialogEdit">编辑</el-button>
      <el-button icon="el-icon-delete" type="success" @click="dialogDel">删除</el-button>
      <el-button icon="el-icon-plus" type="success" @click="dialogInsert">新增表字段</el-button>
      <el-button icon="el-icon-refresh" type="success" @click="dialogFresh">刷新</el-button>
    </el-row>
    <br>

    <el-input v-model="setkeyword" clearable style="width: 195px;" placeholder="请输入关键字" />
    <el-select v-model="fishide" clearable filterable placeholder="请选择" >
      <el-option
        v-for="item in ishideList"
        :key="item.finterid"
        :label="item.fname"
        :value="item.finterid"/>
    </el-select>

    <el-button type="primary" icon="el-icon-search" @click="searchSet">搜索</el-button>

    <el-table
      :data="configData"
      height="450"
      element-loading-text="正在加载"
      border
      fit
      highlight-current-row
      @current-change="configCurrentChange">
      <el-table-column label="记录内码" width="80" align="center">
        <template slot-scope="scope">
          {{ scope.row.FID }}
        </template>
      </el-table-column>
      <el-table-column label="对象代码" width="80" align="center">
        <template slot-scope="scope">
          {{ scope.row.FRepKey }}
        </template>
      </el-table-column>
      <el-table-column label="序号" width="80" align="center">
        <template slot-scope="scope">
          {{ scope.row.FRepIndex }}
        </template>
      </el-table-column>
      <el-table-column label="页号" width="80" align="center">
        <template slot-scope="scope">
          {{ scope.row.FRepPage }}
        </template>
      </el-table-column>
      <el-table-column label="字段标题" align="center">
        <template slot-scope="scope">
          {{ scope.row.FRepCap }}
        </template>
      </el-table-column>
      <el-table-column label="字段名" align="center">
        <template slot-scope="scope">
          {{ scope.row.FRepFld }}
        </template>
      </el-table-column>
      <el-table-column label="字段类型" align="center">
        <template slot-scope="scope">
          {{ scope.row.FRepType }}
        </template>
      </el-table-column>
      <el-table-column label="全部隐藏" width="80" align="center">
        <template slot-scope="scope">
          <el-tag v-if="Number(scope.row.FRepHide) === 1" type="danger">隐藏</el-tag>
          <el-tag v-else type="success">显示</el-tag>
        </template>
      </el-table-column>
      <!--  <el-table-column label="合计" align="center">
        <template slot-scope="scope">
          {{ scope.row.FRepSum }}
        </template>
      </el-table-column> -->
      <el-table-column label="宽度" width="80" align="center">
        <template slot-scope="scope">
          {{ scope.row.FRepWid }}
        </template>
      </el-table-column>
      <el-table-column label="权限键值" width="80" align="center">
        <template slot-scope="scope">
          {{ scope.row.FRightKey }}
        </template>
      </el-table-column>
      <el-table-column label="小数位" width="80" align="center">
        <template slot-scope="scope">
          {{ scope.row.FDecPlaces }}
        </template>
      </el-table-column>
      <el-table-column label="序时薄隐藏" width="95" align="center">
        <template slot-scope="scope">
          <el-tag v-if="Number(scope.row.FRptTypeFlag) === 1" type="danger">序时薄隐藏</el-tag>
          <el-tag v-else type="success">序时薄显示</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="新增修改字段隐藏" width="105" align="center">
        <template slot-scope="scope">
          <el-tag v-if="Number(scope.row.FUnControl) === 1" type="danger">新增修改隐藏</el-tag>
          <el-tag v-else type="success">新增修改显示</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="弹窗表单类型" align="center">
        <template slot-scope="scope">
          <el-tag v-if="Number(scope.row.FRepMeg) === 1" type="danger">下拉框</el-tag>
          <el-tag v-else-if="Number(scope.row.FRepMeg) === 2" type="danger">单选框</el-tag>
          <el-tag v-else-if="Number(scope.row.FRepMeg) === 3" type="danger">多选框</el-tag>
          <el-tag v-else-if="Number(scope.row.FRepMeg) === 4" type="danger">日期框</el-tag>
          <el-tag v-else-if="Number(scope.row.FRepMeg) === 5" type="danger">禁止编辑</el-tag>
          <el-tag v-else-if="Number(scope.row.FRepMeg) === 6" type="danger">多文本框</el-tag>

          <el-tag v-else type="success">输入框</el-tag>
      </template></el-table-column>
      <el-table-column label="备注信息" align="center">
        <template slot-scope="scope">
          {{ scope.row.FRepCap_En }}
        </template>
      </el-table-column>

    </el-table>

    <!-- 添加／编辑 -->
    <el-dialog v-el-drag-dialog :visible.sync="dialogModal" :append-to-body="true" title="添加／编辑">
      <el-form v-loading="loadingForm" ref="form" :rules="rules" :model="form" label-width="140px">
        <el-form-item label="对象代码" prop="FRepKey">
          <el-input v-model="form.FRepKey"/>
        </el-form-item>
        <el-form-item label="序号" prop="FRepIndex">
          <el-input v-model="form.FRepIndex"/>
        </el-form-item>

        <el-form-item label="页号" prop="FRepPage">
          <el-input v-model="form.FRepPage" />
        </el-form-item>
        <el-form-item label="字段标题" prop="FRepCap">
          <el-input v-model="form.FRepCap" />
        </el-form-item>
        <el-form-item label="字段名" prop="FRepFld">
          <el-input v-model="form.FRepFld" />
        </el-form-item>
        <el-form-item label="字段类型" prop="FRepType">
          <el-input v-model="form.FRepType" />
        </el-form-item>
        <el-form-item label="全部隐藏" prop="FRepHide">
          <el-radio-group v-model="form.FRepHide">
            <el-radio :label="0">否</el-radio>
            <el-radio :label="1">是</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="合计" prop="FRepSum">
          <el-input v-model="form.FRepSum" />
        </el-form-item>
        <el-form-item label="宽度" prop="FRepWid">
          <el-input v-model="form.FRepWid" />
        </el-form-item>
        <el-form-item label="权限键值" prop="FRightKey">
          <el-input v-model="form.FRightKey" />
        </el-form-item>
        <el-form-item label="小数位" prop="FDecPlaces">
          <el-input v-model="form.FDecPlaces" />
        </el-form-item>
        <el-form-item label="序时薄隐藏" prop="FRptTypeFlag">
          <el-radio-group v-model="form.FRptTypeFlag">
            <el-radio :label="0">否</el-radio>
            <el-radio :label="1">是</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="新增修改字段隐藏" prop="FUnControl">
          <el-radio-group v-model="form.FUnControl">
            <el-radio :label="0">否</el-radio>
            <el-radio :label="1">是</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="弹窗表单类型" prop="FRepMeg">
          <el-radio-group v-model="form.FRepMeg">
            <el-radio :label="0">输入框</el-radio>
            <el-radio :label="1">下拉框</el-radio>
            <el-radio :label="2">单选框</el-radio>
            <el-radio :label="3">多选框</el-radio>
            <el-radio :label="4">日期框</el-radio>
            <el-radio :label="5">禁止编辑</el-radio>
            <el-radio :label="6">多文本框</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="备注信息" prop="FRepCap_En">
          <el-input v-model="form.FRepCap_En" />
        </el-form-item>

        <el-form-item>
          <el-button :loading="loadingForm" type="primary" @click="confirm" >确认</el-button>
          <el-button @click="dialogModal=false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

  </div>
</template>
<script>
import { configAdd, configUpdate, configDel, insertColumn, tableConfig } from '../../../api/account'
import { common } from '../../../mixins/common'
import { ishide } from '../../../mixins/ishide'

export default {
  name: 'HeadTable',
  mixins: [common, ishide],
  props: {
    configList: {
      type: Array,
      default: () => []
    },
    keyValue: {
      type: Number,
      default: () => 0
    }
  },
  data() {
    return {
      configData: [],
      dialogModal: false,
      loadingForm: false,
      isEditor: false,
      form: {
        FRepKey: '',
        FRepIndex: '',
        FRepPage: '',
        FRepCap: '',
        FRepFld: '',
        FRepType: '',
        FRepHide: 0,
        FRepSum: '',
        FRepWid: '',
        FRightKey: '',
        FDecPlaces: '',
        FRepCap_En: '',
        FUnControl: 0,
        FRepMeg: 0,
        FRptTypeFlag: 0
      },
      rules: {

      },
      curRowData: {},
      keyData: 0,
      setkeyword: ''

    }
  },
  watch: {
    configList(val) {
      this.configData = val
      console.log(val, ' configData')
    },
    keyValue(val) {
      console.log(val, ' val')
      this.keyData = val
    }
  },
  methods: {

    configCurrentChange(val) {
      this.curRowData = val
      // console.log(val, ' val')
    },
    dialogAdd() {
      const data = {
        FRepKey: '',
        FRepIndex: '',
        FRepPage: '',
        FRepCap: '',
        FRepFld: '',
        FRepType: '',
        FRepHide: 0,
        FRepSum: '',
        FRepWid: '',
        FRightKey: '',
        FRepCap_En: '',
        FDecPlaces: '',
        FUnControl: 0,
        FRepMeg: 0,
        FRptTypeFlag: 0
      }
      for (const i in this.form) {
        this.$set(this.form, i, data[i])
      }
      this.form.FRepKey = this.keyData
      console.log(this.form, ' this.form')
      setTimeout(() => {
        this.$refs['form'].clearValidate()
      })

      this.dialogModal = true
    },
    dialogEdit() {
      // console.log(this.curRowData, ' curRowData')
      const data = this.curRowData
      console.log(data, ' data dialogEdit ')
      // console.log(' data dialogEdit ', !data)
      if (data === null || !data.FID) {
        this.$message({
          message: '请选中要编辑的一行',
          type: 'warning'
        })
        return
      }
      this.dialogModal = true
      for (const i in this.form) {
        this.form[i] = data[i]
      }
      // console.log('data：', data)
      // console.log('this.form：', this.form)
      this.form.FID = data.FID
      console.log('this.form：', this.form)
      setTimeout(() => {
        this.$refs['form'].clearValidate()
      })
    },
    async dialogDel() {
      const data = this.curRowData
      if (data === null || !data.FID) {
        this.$message({
          message: '请选中要删除的一行',
          type: 'warning'
        })
        return
      }

      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        cancelButtonClass: 'btn-custom-cancel',
        type: 'warning'
      }).then(async() => {
        console.log(data, ' data')
        const { FID } = data
        const res = await configDel({
          db: this.globalDb.db,
          opername: decodeURIComponent(this.globalDb.fname),
          FID
        })
        if (res.Error === 0) {
          this.$message({
            message: res.Message,
            type: 'success'
          })
          this.$emit('updateConfigData')
          return
        }
        this.$message({
          message: res.Message,
          type: 'error'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    confirm() {
      this.$refs['form'].validate(async(valid) => {
        if (valid) {
          // this.loadingForm = true
          try {
            console.log(this.form, ' this.form')
            let res = ''
            this.form.db = this.globalDb.db
            if (this.form.FID) {
              res = await configUpdate(this.form)
            } else {
              res = await configAdd(this.form)
            }
            if (res.Error === 0) {
              // this.loadingForm = false
              this.dialogModal = false
              console.log(res, ' res add')
              this.$message({
                message: res.Message,
                type: 'success'
              })
              // this.getTableConfig()
              this.$emit('updateConfigData')
            }
          } catch (error) {
            // this.loadingForm = false
            console.log(error)
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 搜索
    async  searchSet() {
      const res = await tableConfig({
        db: this.globalDb.db,
        opername: decodeURIComponent(this.globalDb.fname),
        key: this.keyData,
        ishide: this.fishide,
        keyword: this.setkeyword
      })
      this.configData = res.Data
      // this.$emit('updateConfigData')
    },

    async dialogInsert() {
      const data = this.curRowData
      console.log(data, ' data')
      if (!data && !data.FID) {
        this.$message({
          message: '请选中要编辑的一行',
          type: 'warning'
        })
        return
      }
      const { FRepFld, FRepKey } = data
      const res = await insertColumn({
        db: this.globalDb.db,
        opername: decodeURIComponent(this.globalDb.fname),
        FRepFld,
        FRepKey
      })

      // console.log(res, ' res insert')
      if (res.Error === 0) {
        this.$message({
          message: res.Message,
          type: 'success'
        })
        this.$emit('updateConfigData')
        return
        // this.getTableConfig()
      }
      this.$message({
        message: res.Message,
        type: 'error'
      })
    },

    dialogFresh() {
      this.$emit('updateConfigData')
    }
  }
}
</script>

